﻿@namespace MudBlazor
@inherits MudComponentBase
@typeparam T
@using MudBlazor.Utilities
@using MudBlazor.Extensions
@using MudBlazor.Resources

@inject InternalMudLocalizer Localizer

@{
    _currentRenderFilteredItemsCache = null;
}

<CascadingValue IsFixed="true" Value="this">@Columns</CascadingValue>

<CascadingValue IsFixed="true" Value="this">
    <div @attributes="UserAttributes" class="@_classname" style="@_style">
        @if (Items != null || HasServerData)
        {
            @if (ToolBarContent != null)
            {
                <MudToolBar Class="mud-table-toolbar" Style="@_headerFooterStyle">
                    @ToolBarContent
                    @if (ShowMenuIcon)
                    {
                        @ToolbarMenu(this)
                    }
                </MudToolBar>
            }
            else
            {
                @if (ShowMenuIcon)
                {
                    @*Add the default toolbar.*@
                    <MudToolBar Class="mud-table-toolbar" Style="@_headerFooterStyle">
                        <MudSpacer />
                        @ToolbarMenu(this)
                    </MudToolBar>
                }
            }
            <div @ref=_gridElement class="@_tableClass" style="@_tableStyle">
                <MudDropContainer @ref="_dropContainer" T="Column<T>" Items="@RenderedColumns" ItemsSelector="(item, dropzone) => RenderedColumnsItemsSelector(item, dropzone)" ItemDropped="ItemUpdatedAsync" CanDropClass="@DropAllowedClass" NoDropClass="@DropNotAllowedClass" ApplyDropClassesOnDragStarted="@ApplyDropClassesOnDragStarted">
                    <ChildContent>
                <table class="mud-table-root">
                    @if (ColGroup != null)
                    {
                        <colgroup>
                            @ColGroup
                        </colgroup>
                    }
                    <thead class="@_headClassname">
                        <CascadingValue Name="IsOnlyHeader" IsFixed="true" Value="true">
                            @Header
                        </CascadingValue>
                        <tr class="mud-table-row">
                        @foreach (var column in RenderedColumns)
                        {
                            <HeaderCell SortDirection="@(GetColumnSortDirection(column.PropertyName))" T="T" Column="@column"></HeaderCell>
                        }
                        </tr>                             
                                
                        
                        
                        @if (Filterable && FilterMode == DataGridFilterMode.ColumnFilterRow)
                        {
                            <tr class="mud-table-row">
                                @foreach (var column in RenderedColumns)
                                {
                                    <FilterHeaderCell T="T" Column="@column" />
                                }
                            </tr>
                        }
                    </thead>
                    <tbody class="mud-table-body">
                               
                                @if (_columnsPanelVisible)
                                {
                                    <MudOverlay ZIndex="10" @bind-Visible="_columnsPanelVisible" AutoClose></MudOverlay>

                                    <MudPopover Open="@_columnsPanelVisible" Paper="true" Class="mud-data-grid-columns-panel px-2" OverflowBehavior="OverflowBehavior.FlipAlways" AnchorOrigin="Origin.TopLeft"
                                                TransformOrigin="Origin.TopLeft" Elevation="4"
                                                Style="max-width:700px;z-index:11;" MaxHeight="400">
                                        <MudStack Row="true" Justify="Justify.FlexEnd">
                                            <div style="min-width:250px">
                                                <MudTextField DebounceInterval="500" @bind-Value="@_columnsPanelSearch" OnDebounceIntervalElapsed="@(() => _columnsPanelDropContainer?.Refresh())"
                                                                Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search" Margin="Margin.Dense" FullWidth="true" />
                                            </div>
                                        </MudStack>

                                        <MudDropContainer @ref="_columnsPanelDropContainer" T="Column<T>" Items="@RenderedColumns" ItemsSelector="(item, dropzone) => true" 
                                            ItemDisabled="@((item) => !this.ColumnsPanelReordering)" ItemDropped="ColumnOrderUpdated">
                                            <ChildContent>
                                                <MudDropZone T="Column<T>" Class="flex-grow-1" AllowReorder=this.ColumnsPanelReordering />
                                            </ChildContent>
                                            <ItemRenderer>
                                                @if (string.IsNullOrEmpty(_columnsPanelSearch) || context.Title?.Contains(_columnsPanelSearch, StringComparison.InvariantCultureIgnoreCase) == true)
                                                {
                                                    <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center" Style="border-bottom: 1px solid var(--mud-palette-table-lines);">
                                                        @if (ColumnsPanelReordering)
                                                        {
                                                            <MudStack Spacing="0">
                                                                @if (RenderedColumns.IndexOf(context) != 0)
                                                                {
                                                                    <MudIconButton Ripple="false" Icon="@Icons.Material.Filled.ArrowDropUp" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_MoveUp] Disabled="@(RenderedColumns.IndexOf(context) == 0)" OnClick="@(e => ColumnUp(context))"></MudIconButton>
                                                                }
                                                                @if (RenderedColumns.IndexOf(context) != RenderedColumns.Count - 1)
                                                                {
                                                                    <MudIconButton Ripple="false" Icon="@Icons.Material.Filled.ArrowDropDown" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_MoveDown] Disabled="@(RenderedColumns.IndexOf(context) == RenderedColumns.Count - 1)" OnClick="@(e => ColumnDown(context))"></MudIconButton>
                                                                }
                                                            </MudStack>
                                                        }

                                                        @if (context?.GroupingState.Value ?? false)
                                                        {
                                                            <MudIconButton Ripple="false" Icon="@Icons.Material.Filled.AccountTree" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Ungroup] Disabled="!context.groupable" OnClick="@(e => context?.HeaderCell.UngroupColumnAsync())"></MudIconButton>
                                                        }
                                                        else
                                                        {
                                                            <MudIconButton Ripple="false" Icon="@Icons.Material.Filled.TableRows" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Group] Disabled="!context.groupable" OnClick="@(e => context?.HeaderCell.GroupColumnAsync())"></MudIconButton>
                                                        }
                                                        <MudSwitch T="bool" Ripple="false" Class="flex-grow-1" Color="Color.Primary" Disabled="@(!context.hideable)" Value="@context.HiddenState.Value" ValueChanged="@context.ToggleAsync" Converter="_oppositeBoolConverter" Label="@context.Title" />
                                                        <MudSpacer />

                                                        <div>
                                                            <MudIconButton Ripple="false" Icon="@context.SortIcon" Class="@context.HeaderCell.sortIconClass" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Sort] Disabled="!context.sortable" OnClick="@(e => context?.HeaderCell.SortChangedAsync(e))"></MudIconButton>
                                                            @if (context.DataGrid.SortMode == SortMode.Multiple)
                                                            {
                                                                if (context.SortIndex < 0)
                                                                {
                                                                    <span class="mud-sort-index mud-text-disabled invisible">@(context.SortIndex + 1)</span>
                                                                }
                                                                else
                                                                {
                                                                    <span class="mud-sort-index mud-text-disabled">@(context.SortIndex + 1)</span>
                                                                }
                                                            }
                                                        </div>

                                                        @if (context.HeaderCell.hasFilter)
                                                        {
                                                            <MudIconButton Ripple="false" Class="filter-button filtered" Icon="@Icons.Material.Filled.FilterAlt" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Filter] Disabled="!context.filterable" OnClick="@(e => context?.HeaderCell.OpenFilters())"></MudIconButton>
                                                        }
                                                        else
                                                        {
                                                            <MudIconButton Ripple="false" Class="filter-button" Icon="@Icons.Material.Outlined.FilterAlt" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Filter] Disabled="!context.filterable" OnClick="@(e => context?.HeaderCell.AddFilter())"></MudIconButton>
                                                        }

                                                        @if (ColumnsPanelReordering)
                                                        {
                                                            <MudIconButton Ripple="false" Icon="@context.DataGrid.DragIndicatorIcon" Size="@Size.Small" Class="drag-icon-options"></MudIconButton>
                                                        }
                                                    </MudStack>
                                                }
                                            </ItemRenderer>
                                        </MudDropContainer>
                                        <MudStack Row="true">
                                            @if (Groupable)
                                            {
                                                <MudButton OnClick="@CollapseAllGroups" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_CollapseAllGroups]</MudButton>
                                                <MudButton OnClick="@ExpandAllGroups" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_ExpandAllGroups]</MudButton>
                                            }
                                            <MudSpacer />

                                            <MudButton OnClick="@HideAllColumnsAsync" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_HideAll]</MudButton>
                                            <MudButton OnClick="@ShowAllColumnsAsync" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_ShowAll]</MudButton>
                                        </MudStack>
                                    </MudPopover>
                                }
                                @if (_filtersMenuVisible && FilterMode == DataGridFilterMode.Simple)
                                {
                                    <MudOverlay ZIndex="12" @bind-Visible="@_filtersMenuVisible" AutoClose></MudOverlay>
                                    <MudPopover Open="@_filtersMenuVisible" Paper="true" Class="filters-panel pa-2" OverflowBehavior="OverflowBehavior.FlipAlways" AnchorOrigin="Origin.TopLeft"
                                                TransformOrigin="Origin.TopLeft" Elevation="4" Style="width:700px;max-width:700px;z-index:13;" MaxHeight="400">
                                        @if (FilterTemplate == null)
                                        {
                                            <MudStack>
                                                @foreach (var f in FilterDefinitions)
                                                {
                                                    @Filter(f, null)
                                                }
                                            </MudStack>
                                            <MudButton Class="mt-2" StartIcon="@Icons.Material.Filled.Add" OnClick="@AddFilter" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_AddFilter]</MudButton>
                                            @if (HasServerData)
                                            {
                                                <MudButton Class="mt-2" StartIcon="@Icons.Material.Filled.Add" OnClick="@ApplyFiltersAsync" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_Apply]</MudButton>
                                            }
                                            <MudButton Class="mt-2" StartIcon="@Icons.Material.Filled.Remove" OnClick="@ClearFiltersAsync" Color="@Color.Primary">@Localizer[LanguageResource.MudDataGrid_Clear]</MudButton>
                                        }
                                        else
                                        {
                                            @FilterTemplate(this)
                                        }
                                    </MudPopover>
                                }
                        @if (Loading)
                        {
                            <tr>
                                <td colspan="1000" class="mud-table-loading">
                                    <MudProgressLinear Color="@LoadingProgressColor" Class="mud-table-loading-progress" Indeterminate="true" />
                                </td>
                            </tr>
                        }

                        @{var resolvedPageItems = CurrentPageItems.ToList();}
                        @if (resolvedPageItems is { Count: > 0 })
                        {
                            if (GroupedColumn != null)
                            {
                                foreach (var g in _currentPageGroups)
                                {
                                    <tr class="mud-table-row">
                                        @{ var groupClass = new CssBuilder(GroupClass).AddClass(GroupClassFunc?.Invoke(g)).Build(); }
                                        @{ var groupStyle = new StyleBuilder().AddStyle(GroupStyle).AddStyle(GroupStyleFunc?.Invoke(g)).Build(); }

                                        <td class="mud-table-cell @groupClass" colspan="1000" style="background-color:var(--mud-palette-background-gray);@groupStyle">
                                            <MudIconButton Class="mud-table-row-expander"
                                           Icon="@(g.Expanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ChevronRight)"
                                           OnClick="@(() => ToggleGroupExpansion(g))" />

                                            @if (GroupedColumn.GroupTemplate == null)
                                            {
                                                <span style="font-weight:bold">@GroupedColumn.Title: @g.Grouping.Key</span>
                                            }
                                            else
                                            {
                                                @GroupedColumn.GroupTemplate(@g)
                                            }
                                        </td>
                                    </tr>
                                    var rowIndex = 0;

                                    @if (g.Expanded)
                                    {
                                        <MudVirtualize @ref="_mudVirtualize" ItemsProvider="@VirtualItemsProvider" Enabled="@Virtualize" Items="@g.Grouping.ToList()" OverscanCount="@OverscanCount" ItemSize="@ItemSize" Context="item">
                                            <Placeholder>
                                                @if (RowLoadingContent != null)
                                                {
                                                    @RowLoadingContent
                                                }
                                                else
                                                {
                                                    <tr class="mud-table-row">
                                                        <td class="mud-table-cell" colspan="1000">
                                                            @Localizer[LanguageResource.MudDataGrid_Loading]
                                                        </td>
                                                    </tr>
                                                }
                                            </Placeholder>
                                            
                                            <ChildContent>
                                                @{ var rowClass = new CssBuilder(RowClass).AddClass(RowClassFunc?.Invoke(item, rowIndex)).Build(); }
                                                @{ var rowStyle = new StyleBuilder().AddStyle(RowStyle).AddStyle(RowStyleFunc?.Invoke(item, rowIndex)).Build(); }
                                                @{ var tmpRowIndex = rowIndex; }
                                                <tr class="mud-table-row @rowClass" Style="@rowStyle" @key="item" 
                                                    @onclick="@((args) => OnRowClickedAsync(args, item, tmpRowIndex))" 
                                                    @oncontextmenu="@((args) => OnContextMenuClickedAsync(args, item, tmpRowIndex))"
                                                    @oncontextmenu:preventDefault="@(RowContextMenuClick.HasDelegate ? true : false)">
                                                    <CascadingValue Value="@Validator" IsFixed="true">
                                                        @foreach (var column in RenderedColumns)
                                                        {
                                                            if (!column.HiddenState.Value)
                                                            {
                                                                @Cell(column, item)
                                                            }
                                                        }
                                                    </CascadingValue>
                                                </tr>
                                                @if (ChildRowContent != null && (_openHierarchies.Contains(item) || !hasHierarchyColumn))
                                                {
                                                    <tr class="mud-table-row">
                                                        <td class="mud-table-cell" colspan="1000">
                                                            @ChildRowContent(new CellContext<T>(this, item))
                                                        </td>
                                                    </tr>
                                                }
                                                @{ rowIndex++; }
                                            </ChildContent>
                                        </MudVirtualize>
                                        @*Group Footer*@
                                        <tr class="mud-table-row">
                                            @FooterCells(g.Grouping.ToList())
                                        </tr>
                                    }
                                }
                            }
                            else
                            {
                                var rowIndex = 0;
                                <MudVirtualize @ref="_mudVirtualize" ItemsProvider="@VirtualItemsProvider" Enabled="@Virtualize" Items="@resolvedPageItems" OverscanCount="@OverscanCount" ItemSize="@ItemSize" Context="item">
                                    <Placeholder>
                                        @if (RowLoadingContent != null)
                                        {
                                            @RowLoadingContent
                                        }
                                        else
                                        {
                                            <tr class="mud-table-row">
                                                <td class="mud-table-cell" colspan="1000">
                                                    @Localizer[LanguageResource.MudDataGrid_Loading]
                                                </td>
                                            </tr>
                                        }
                                    </Placeholder>

                                    <ChildContent>
                                        @{ var rowClass = new CssBuilder(RowClass).AddClass(RowClassFunc?.Invoke(item, rowIndex)).Build(); }
                                        @{ var rowStyle = new StyleBuilder().AddStyle(RowStyle).AddStyle(RowStyleFunc?.Invoke(item, rowIndex)).Build(); }
                                        @{ var tmpRowIndex = rowIndex; }
                                        <tr class="mud-table-row @rowClass" Style="@rowStyle" @key="item" 
                                            @onclick="@((args) => OnRowClickedAsync(args, item, tmpRowIndex))" 
                                            @oncontextmenu="@((args) => OnContextMenuClickedAsync(args, item, tmpRowIndex))"
                                            @oncontextmenu:preventDefault="@(RowContextMenuClick.HasDelegate ? true : false)">
                                            <CascadingValue Value="@Validator" IsFixed="true">
                                                @foreach (var column in RenderedColumns)
                                                {
                                                    if (!column.HiddenState.Value)
                                                    {
                                                        @Cell(column, item)
                                                    }
                                                }
                                            </CascadingValue>
                                        </tr>
                                        @if (ChildRowContent != null && (_openHierarchies.Contains(item) || !hasHierarchyColumn))
                                        {
                                            <tr class="mud-table-row">
                                                <td class="mud-table-cell" colspan="1000">
                                                    @ChildRowContent(new CellContext<T>(this, item))
                                                </td>
                                            </tr>
                                        }
                                        @{ rowIndex++; }
                                    </ChildContent>
                                </MudVirtualize>
                            }
                        }
                        else if (Loading ? LoadingContent != null : NoRecordsContent != null)
                        {
                            <tr>
                                <th colspan="1000" class="mud-table-empty-row">
                                    <div Class="my-3">
                                        @if (Loading)
                                        {
                                            @LoadingContent
                                        }
                                        else
                                        {
                                            @NoRecordsContent
                                        }
                                    </div>
                                </th>
                            </tr>
                        }
                    </tbody>
                    <tfoot class="@_footClassname">
                        <tr class="mud-table-row">
                            @FooterCells(resolvedPageItems)
                        </tr>
                    </tfoot>
                </table>
                    </ChildContent>
                    <ItemRenderer>
                        @context.HeaderCell.TableHeader()
                    </ItemRenderer>
                </MudDropContainer>
            </div>
            @if (PagerContent != null)
            {
                <div class="mud-table-pagination" style="@_headerFooterStyle">
                    @PagerContent
                </div>
            }
        }
    </div>
    <CascadingValue Value="true" IsFixed Name="IsNested">
        <MudDialog Options="EditDialogOptions" @bind-Visible="isEditFormOpen">
            <DialogContent>
                <MudForm @ref="_editForm" FieldChanged="FormFieldChanged">
                    @foreach (var column in RenderedColumns)
                    {
                        var cell = new Cell<T>(this, column, _editingItem);

                        if (column.EditTemplate != null)
                        {
                            @column.EditTemplate(cell._cellContext)
                        }
                        else
                        {
                            var propertyType = column.PropertyType;

                            if (propertyType == typeof(string))
                            {
                                <MudTextField T="string" Label="@column.Title" Value="@cell._valueString" ValueChanged="@cell.StringValueChangedAsync" Margin="@Margin.Dense"
                                  Required=column.Required Variant="@Variant.Outlined" Disabled="@(!column.Editable || ReadOnly)" Class="mt-4" />
                            }
                            else if (TypeIdentifier.IsNumber(propertyType))
                            {
                                <MudNumericField T="double?" Label="@column.Title" Value="@cell._valueNumber" ValueChanged="@cell.NumberValueChangedAsync" Margin="@Margin.Dense"
                                     Required=column.Required Variant="@Variant.Outlined" Disabled="@(!column.Editable || ReadOnly)" Class="mt-4" />
                            }
                        }
                    }
                </MudForm>
            </DialogContent>
            <DialogActions>
                <MudButton Variant="Variant.Filled" Color="Color.Default" OnClick="@CancelEditingItemAsync" Class="px-10">@Localizer[LanguageResource.MudDataGrid_Cancel]</MudButton>
                <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@CommitItemChangesAsync" Class="px-10">@Localizer[LanguageResource.MudDataGrid_Save]</MudButton>
            </DialogActions>
        </MudDialog>
    </CascadingValue>
</CascadingValue>

@code {

    internal RenderFragment ToolbarMenu(MudDataGrid<T> dataGrid) =>
        @<text>
            <MudMenu Icon="@Icons.Material.Outlined.Settings" Size="Size.Small" AnchorOrigin="Origin.BottomCenter" Dense="true">
            <MudMenuItem OnClick="@dataGrid.ShowColumnsPanel">@Localizer[LanguageResource.MudDataGrid_Columns]</MudMenuItem>
                @if (dataGrid.Groupable)
                {
                    <MudMenuItem OnClick="@dataGrid.ExpandAllGroups">@Localizer[LanguageResource.MudDataGrid_ExpandAllGroups]</MudMenuItem>
                    <MudMenuItem OnClick="@dataGrid.CollapseAllGroups">@Localizer[LanguageResource.MudDataGrid_CollapseAllGroups]</MudMenuItem>
                }
                @if (dataGrid.HasServerData)
                {
                    <MudMenuItem OnClick="@dataGrid.InvokeServerLoadFunc">@Localizer[LanguageResource.MudDataGrid_RefreshData]</MudMenuItem>
                }
            </MudMenu>
         </text>;

    internal RenderFragment FooterCells(IEnumerable<T> currentItems) =>
        @<text>
            @if (currentItems is not null)
            {
                foreach (var column in RenderedColumns)
                {
                    if (!column.HiddenState.Value)
                    {
                        if (column.AggregateDefinition is not null || column.FooterTemplate is not null || hasFooter)
                        {
                            <FooterCell T="T" Column="@column" CurrentItems="@currentItems"></FooterCell>
                        }
                    }
                }
            }
         </text>;
    

    internal RenderFragment Cell(Column<T> column, T item) =>
        @<text>
            @{
                var cell = new Cell<T>(this, column, item);
            }
            <td data-label="@column.Title" class="@cell.computedClass" style="@cell.computedStyle">
                @if (column.Editable && !ReadOnly && EditMode == DataGridEditMode.Cell)
                {
                    if (column.EditTemplate is not null)
                    {
                        @column.EditTemplate(cell._cellContext)
                    }
                    else
                    {
                        if (column.PropertyType == typeof(string))
                        {
                            <MudTextField T="string"
                                          Value="@cell._valueString"
                                          ValueChanged="@cell.StringValueChangedAsync"
                                          Margin="@Margin.Dense" Style="margin-top:0"
                                          Required=column.Required
                                          Variant="@Variant.Text" />
                        }
                        else if (column.isNumber)
                        {
                            <MudNumericField T="double?"
                                             Value="@cell._valueNumber"
                                             ValueChanged="@cell.NumberValueChangedAsync"
                                             Margin="@Margin.Dense" Style="margin-top:0"
                                             Required=column.Required Variant="@Variant.Text" Culture="@column.Culture" />
                        }
                    }
                }
                else
                {
                    if (column.CellTemplate is not null)
                    {
                        @column.CellTemplate(cell._cellContext)
                    }
                    else if (column.Culture is not null && column.isNumber)
                    {
                        if (column.ContentFormat is not null)
                        {
                            @(((IFormattable)cell._valueNumber)?.ToString(column.ContentFormat, column.Culture))
                        }
                        else
                        {
                            @cell._valueNumber?.ToString(column.Culture)
                        }
                    }
                    else
                    {
                        if (column.ContentFormat is not null)
                        {
                            @(((IFormattable)cell.ComputedValue)?.ToString(column.ContentFormat, column.Culture))
                        }
                        else
                        {
                            @cell.ComputedValue
                        }
                    }
                }
            </td>
         </text>;

    internal RenderFragment Filter(IFilterDefinition<T> filterDefinition, Column<T> column) =>
        @<text>
            @{
                var filter = new Filter<T>(this, filterDefinition, column);
                var fieldType = filterDefinition.FieldType;
            }
            @if (column is null)
            {
                <MudGrid Spacing="0">
                    <MudItem xs="1" Class="d-flex">
                        <MudIconButton Class="remove-filter-button" Icon="@Icons.Material.Filled.Close" OnClick="@filter.RemoveFilterAsync" Size="@Size.Small" Style="align-self: flex-end"></MudIconButton>
                    </MudItem>
                    <MudItem xs="4">
                        <MudSelect T="Column<T>" Value="@filterDefinition.Column" ValueChanged="@filter.FieldChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Column]" Dense="true" Margin="@Margin.Dense"
                                   Class="filter-field">
                            @foreach (var renderColumn in RenderedColumns.Where(x => x.Filterable != false) ?? Enumerable.Empty<Column<T>>())
                            {
                                <MudSelectItem T="Column<T>" Value="@renderColumn">@renderColumn.Title</MudSelectItem>
                            }
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="3">
                        <MudSelect @bind-Value="filterDefinition.Operator" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Operator]" Dense="true" Margin="@Margin.Dense"
                                   Class="filter-operator">
                            @foreach (var fieldOperator in FilterOperator.GetOperatorByDataType(fieldType))
                            {
                                <MudSelectItem Value="@fieldOperator">@Localizer[FilterOperator.GetTranslationKeyByOperatorName(fieldOperator)]</MudSelectItem>
                            }
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="4">
                        @if (fieldType.IsString && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudTextField T="string" Value="@filter._valueString" ValueChanged="@filter.StringValueChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                          Immediate="true" Class="filter-input" />
                        }
                        else if (fieldType.IsNumber && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudNumericField T="double?" Value="@filter._valueNumber" ValueChanged="@filter.NumberValueChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                             Immediate="true" Class="filter-input" Culture="@filter.FilterColumn?.Culture"/>
                        }
                        else if (fieldType.IsEnum)
                        {
                            <MudSelect T="Enum" Value="@filter._valueEnum" ValueChanged="@filter.EnumValueChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Dense="true" Margin="@Margin.Dense"
                                       Class="filter-input">
                                <MudSelectItem T="Enum" Value="@(null)"></MudSelectItem>
                                @foreach (var item in EnumExtensions.GetSafeEnumValues(fieldType.InnerType))
                                {
                                    <MudSelectItem T="Enum" Value="@((Enum)item)">@item</MudSelectItem>
                                }
                            </MudSelect>
                        }
                        else if (fieldType.IsBoolean)
                        {
                            <MudSelect T="bool?" Value="@filter._valueBool" ValueChanged="@filter.BoolValueChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Dense="true" Margin="@Margin.Dense"
                                       Class="filter-input">
                                <MudSelectItem T="bool?" Value="@(null)"></MudSelectItem>
                                <MudSelectItem T="bool?" Value="@(true)">@Localizer[LanguageResource.MudDataGrid_True]</MudSelectItem>
                                <MudSelectItem T="bool?" Value="@(false)">@Localizer[LanguageResource.MudDataGrid_False]</MudSelectItem>
                            </MudSelect>
                        }
                        else if (fieldType.IsDateTime && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudGrid Spacing="0">
                                <MudItem xs="7">
                                    <MudDatePicker Date="@filter._valueDate" DateChanged="@filter.DateValueChanged" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense" Class="filter-input-date"/>
                                </MudItem>
                                <MudItem xs="5">
                                    <MudTimePicker Time="@filter._valueTime" TimeChanged="@filter.TimeValueChanged" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense" Class="filter-input-time" />
                                </MudItem>
                            </MudGrid>
                        }
                        else if (fieldType.IsGuid)
                        {
                            <MudTextField T="Guid?" Value="@filter._valueGuid" ValueChanged="@filter.GuidValueChanged" FullWidth="true" Label="@Localizer[LanguageResource.MudDataGrid_Value]" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                          Immediate="true" Class="filter-input"/>
                        }
                    </MudItem>
                </MudGrid>

            }
            else
            {
                <MudGrid Spacing="0">
                    <MudItem xs="12">
                        <MudSelect @bind-Value="filterDefinition.Operator" FullWidth="true" Dense="true" Margin="@Margin.Dense"
                                   Class="filter-operator">
                            @foreach (var o in FilterOperator.GetOperatorByDataType(fieldType))
                            {
                                <MudSelectItem Value="@o">@Localizer[FilterOperator.GetTranslationKeyByOperatorName(o)]</MudSelectItem>
                            }
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="12">
                        @if (fieldType.IsString && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudTextField T="string" Value="@filter._valueString" ValueChanged="@filter.StringValueChanged" FullWidth="true" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                          Immediate="true" Class="filter-input"/>
                        }
                        else if (fieldType.IsNumber && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudNumericField T="double?" Value="@filter._valueNumber" ValueChanged="@filter.NumberValueChanged" FullWidth="true" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                             Immediate="true" Class="filter-input" Culture="@filter.FilterColumn?.Culture"/>
                        }
                        else if (fieldType.IsEnum)
                        {
                            <MudSelect T="Enum" Value="@filter._valueEnum" ValueChanged="@filter.EnumValueChanged" FullWidth="true" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Dense="true" Margin="@Margin.Dense"
                                       Class="filter-input">
                                <MudSelectItem T="Enum" Value="@(null)"></MudSelectItem>
                                @foreach (var item in EnumExtensions.GetSafeEnumValues(fieldType.InnerType))
                                {
                                    <MudSelectItem T="Enum" Value="@((Enum)item)">@item</MudSelectItem>
                                }
                            </MudSelect>
                        }
                        else if (fieldType.IsBoolean)
                        {
                            <MudSelect T="bool?" Value="@filter._valueBool" ValueChanged="@filter.BoolValueChanged" FullWidth="true" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Dense="true" Margin="@Margin.Dense"
                                       Class="filter-input">
                                <MudSelectItem T="bool?" Value="@(null)"></MudSelectItem>
                                <MudSelectItem T="bool?" Value="@(true)">@Localizer[LanguageResource.MudDataGrid_True]</MudSelectItem>
                                <MudSelectItem T="bool?" Value="@(false)">@Localizer[LanguageResource.MudDataGrid_False]</MudSelectItem>
                            </MudSelect>
                        }
                        else if (fieldType.IsDateTime && !(filterDefinition.Operator ?? "").EndsWith("empty"))
                        {
                            <MudGrid Spacing="0">
                                <MudItem xs="7">
                                    <MudDatePicker Date="@filter._valueDate" DateChanged="@filter.DateValueChanged" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense" Class="filter-input-date"/>
                                </MudItem>
                                <MudItem xs="5">
                                    <MudTimePicker Time="@filter._valueTime" TimeChanged="@filter.TimeValueChanged" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense" Class="filter-input-time"/>
                                </MudItem>
                            </MudGrid>
                        }
                        else if (fieldType.IsGuid)
                        {
                            <MudTextField T="Guid?" Value="@filter._valueGuid" ValueChanged="@filter.GuidValueChanged" FullWidth="true" Placeholder="@Localizer[LanguageResource.MudDataGrid_FilterValue]" Margin="@Margin.Dense"
                                          Immediate="true" Class="filter-input"/>
                        }
                    </MudItem>
                </MudGrid>
            }
         </text>;
}